package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.PermissionDao;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 */
@Service
@Slf4j
public class PermissionServiceImpl implements PermissionService {


    @Autowired
    private PermissionDao  permissionDao;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[权限-分页展示处理层]data:{}", queryPageBean);

        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //分页查询数据库
        Page<Permission> page =permissionDao.findPage(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 添加权限
     * @param permission
     */
    @Override
    @Transactional
    public void add(Permission permission) {

        permissionDao.add(permission);

    }

    /**
     * 根据id查询权限
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        log.info("[权限-根据ID查询]id:{}",id);
        return permissionDao.selectById(id);

    }

    /**
     * 编辑权限
     * @param permission
     */

    @Override
    @Transactional
    public void edit(Permission permission) {

        permissionDao.update(permission) ;

    }
    /**
     * 根据id删除
     */
    @Override
    @Transactional
    public void deleteById(Integer id) {

        //校验是否可以删除
        Long count = permissionDao.countPermissionByRole(id);
        if (count > 0) {
            throw new BusinessRuntimeException("当前权限有关联数据，不能删除");
        }

        //实际删除操作
        permissionDao.deleteById(id);

    }



    @Override
    public List<Permission> fandAll() {
        log.info("[权限-信息查询]data:{}");
        return permissionDao.selectpermission();

    }
}
